package com.czxy.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czxy.domain.*;
import com.czxy.enterprise.feign.UserFeign;
import com.czxy.enterprise.mapper.EnterpriseMapper;
import com.czxy.enterprise.service.EnNatureService;
import com.czxy.enterprise.service.EnPhotoService;
import com.czxy.enterprise.service.EnterpriseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.enterprise.service.PhotoService;
import com.czxy.vo.EnterpriseVo;
import org.apache.commons.lang3.StringUtils;
import org.apache.tomcat.util.digester.ObjectCreateRule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author xuewen
 * @since 2022-01-13
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, TbEnterprise> implements EnterpriseService {
    @Autowired
    private EnPhotoService enPhotoService;
    @Autowired
    private PhotoService photoService;
    @Autowired
    private EnNatureService enNatureService;
    @Autowired
    private UserFeign userFeign;
    @Autowired
    private RecruitmentInfoServiceImpl recruitmentInfoService;

    @Override
    public TbEnterprise findById(Integer id) {
        TbEnterprise tbEnterprise = this.baseMapper.selectById(id);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enterprise_id",id);
        List<TbEnPhoto> list = enPhotoService.list(queryWrapper);
        List<TbPhoto> photoList = new ArrayList<>();
        for (TbEnPhoto tbEnPhoto : list) {
            TbPhoto photo = photoService.getById(tbEnPhoto.getPhotoId());
            photoList.add(photo);
        }
        tbEnterprise.setTbPhotoList(photoList);
        TbEnNature tbEnNature = enNatureService.getById(tbEnterprise.getNatureId());
        tbEnterprise.setTbEnNature(tbEnNature);
        TbUser tbUser = userFeign.findById(tbEnterprise.getUserId()).getData();
        tbEnterprise.setTbUser(tbUser);
        return tbEnterprise;
    }

    @Override
    public List<TbEnterprise> findByUserId(Integer id,Integer state) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id",id);
        queryWrapper.eq("state",state);
        List<TbEnterprise> list = this.baseMapper.selectList(queryWrapper);
        for (TbEnterprise tbEnterprise : list) {
            QueryWrapper queryWrapperPhoto = new QueryWrapper();
            queryWrapperPhoto.eq("enterprise_id",tbEnterprise.getId());
            List<TbEnPhoto> photos = enPhotoService.list(queryWrapperPhoto);
            List<TbPhoto> photoList = new ArrayList<>();
            for (TbEnPhoto tbEnPhoto : photos) {
                TbPhoto photo = photoService.getById(tbEnPhoto.getPhotoId());
                photoList.add(photo);
            }
           if (photoList.size()>0){
               tbEnterprise.setTbPhoto(photoList.get(0));
           }
        }
        return list;
    }

    @Override
    public Page findEnterpriseByState(EnterpriseVo enterpriseVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (enterpriseVo.getState()!=null){
            queryWrapper.eq("state",enterpriseVo.getState());
        }else {
            ArrayList<Integer> list = new ArrayList<>();
            list.add(2);
            list.add(3);
            queryWrapper.in("state",list);
        }
        Page<TbEnterprise> page = new Page(enterpriseVo.getCurrent(),enterpriseVo.getSize());
        this.baseMapper.selectPage(page,queryWrapper);
        for (TbEnterprise tbEnterprise : page.getRecords()) {
            TbUser tbUser = userFeign.findById(tbEnterprise.getUserId()).getData();
            tbEnterprise.setTbUser(tbUser);
            TbEnNature tbEnNature = enNatureService.getById(tbEnterprise.getNatureId());
            tbEnterprise.setTbEnNature(tbEnNature);
        }
        return page;
    }

    @Override
    public void ban(Integer id) {
        TbEnterprise tbEnterprise = this.baseMapper.selectById(id);
        tbEnterprise.setState(4);
        this.baseMapper.updateById(tbEnterprise);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enterprise_id",tbEnterprise.getId());
        List<TbRecruitmentInfo> list = recruitmentInfoService.list(queryWrapper);
        List<Integer> ids = list.stream().map(tbRecruitmentInfo -> tbRecruitmentInfo.getId()).collect(Collectors.toList());
        recruitmentInfoService.removeByIds(ids);
    }

    @Override
    public Page condition(EnterpriseVo enterpriseVo) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(enterpriseVo.getName()),"name",enterpriseVo.getName());
        queryWrapper.like(StringUtils.isNotBlank(enterpriseVo.getPhone()),"enPhone",enterpriseVo.getPhone());
        queryWrapper.like(StringUtils.isNotBlank(enterpriseVo.getRegistrationNo()),"registration_no",enterpriseVo.getRegistrationNo());
        queryWrapper.ge(StringUtils.isNotBlank(enterpriseVo.getState()),"state",enterpriseVo.getState());

        Page<TbEnterprise> page = new Page(enterpriseVo.getCurrent(),enterpriseVo.getSize());
        this.baseMapper.selectPage(page,queryWrapper);
        for (TbEnterprise tbEnterprise : page.getRecords()) {
            TbUser tbUser = userFeign.findById(tbEnterprise.getUserId()).getData();
            tbEnterprise.setTbUser(tbUser);
            TbEnNature tbEnNature = enNatureService.getById(tbEnterprise.getNatureId());
            tbEnterprise.setTbEnNature(tbEnNature);
        }
        return page;
    }
}
